﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReconCaracteres_Backpropagation
{
    class MyFile
    {
        public TextWriter file;
        public MyFile(String fileName)
        {
            file = new StreamWriter(fileName);
        }

        public void writeMatrix(Double[,] matrix)
        {

            for(int i=0; i<matrix.GetLength(0); i++){
                for (int j = 0; j < matrix.GetLength(1); j++){
                    file.Write(matrix[j,i]);
                    if (j != matrix.GetLength(1) - 1)
                    {
                        file.Write(" ");
                    }
                  
                }


                if (i != matrix.GetLength(0)-1)
                {
                    file.WriteLine("");
                }
            }
        }
        public void writeSeparator(int sampleIndexID)
        {
            file.WriteLine("");
            file.WriteLine("["+sampleIndexID+"]");

        }
        public void writeWeights(int inputLayerNeuronsAmount, int hiddenLayerNeuronsAmount, int outputLayerNeuronsAmount)
        {
            Random randNum = new Random();

             // No larger than 108, no smaller than 1

            for (int i = 0; i < inputLayerNeuronsAmount; i++)
            {
                for (int j = 0; j < hiddenLayerNeuronsAmount-1; j++)
                {
                   // file.Write(randNum.Next(-300, 300)*.001 + " ");
                    if (j < hiddenLayerNeuronsAmount - 2)
                    {

                        file.Write(randNum.Next(-300, 300) * .001 + " ");
                    }
                    else
                    {
                        file.Write(randNum.Next(-300, 300) * .001);
                    }

                }
                file.WriteLine("");
            }
            file.WriteLine("*");
            for (int i = 0; i < hiddenLayerNeuronsAmount; i++)
            {
                for (int j = 0; j < outputLayerNeuronsAmount - 1; j++)
                {
                    if (j < outputLayerNeuronsAmount - 2)
                    {

                        file.Write(randNum.Next(-300, 300) * .001 + " ");
                    }
                    else
                    {
                        file.Write(randNum.Next(-300, 300) * .001);
                    }

                }
                if (i < hiddenLayerNeuronsAmount-1){
                    file.WriteLine("");
                }
            }
        }
        public void writeWeights(BackPropagationNetwork b)
        {
            Random randNum = new Random();

            // No larger than 108, no smaller than 1

            for (int i = 0; i < b.inputLayer.neurons.Count(); i++)
            {
                for (int j = 0; j < b.inputLayer.neurons[0].weights.Count(); j++)
                {

                    if (j < b.inputLayer.neurons[0].weights.Count() - 1)
                    {
                        file.Write(b.inputLayer.neurons[i].weights[j]+" ");

                    }
                    else
                    {
                        file.Write(b.inputLayer.neurons[i].weights[j]);
                    }

                }
                file.WriteLine("");
            }
            file.WriteLine("*");
            for (int i = 0; i < b.hiddenLayer.neurons.Count(); i++)
            {
                for (int j = 0; j <  b.hiddenLayer.neurons[0].weights.Count() ; j++)
                {
                    if (j < b.hiddenLayer.neurons[0].weights.Count() - 1)
                    {
                        file.Write(b.hiddenLayer.neurons[i].weights[j] + " ");

                    }
                    else
                    {
                        file.Write(b.hiddenLayer.neurons[i].weights[j]);
                    }

                    //file.Write(b.hiddenLayer.neurons[i].weights[j] + " ");
                    /*if (j < outputLayerNeuronsAmount - 2)
                    {

                        file.Write(randNum.Next(-300, 300) * .001 + " ");
                    }
                    else
                    {
                        file.Write(randNum.Next(-300, 300) * .001);
                    }*/

                }
                file.WriteLine("");
              /*  if (i < hiddenLayerNeuronsAmount - 1)
                {
                    file.WriteLine("");
                }*/
            }

        }
        public void close()
        {
            file.Close();
        }

    }
}
